layui.use(['table'], function () {
    const table = layui.table,
        $ = layui.jquery;

    // 创建渲染实例
    table.render({
        elem: '#ID-material-table-edit',
        url: URL_SYSTEM_MATERIAL,
        height: 'full-140',
        cellMinWidth: 80,
        page: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'materialCode', title: '材料编码', width: 220},
            {field: 'materialName', title: '材料名称', sort: true},
            {field: 'materialPrice', title: '材料价格', width: 120, sort: true},
            {field: 'materialNum', title: '库存数量', width: 120, sort: true}
        ]],
        done: function (res, curr, count) {
            // 隐藏全选框
            $(".layui-table-header .laytable-cell-checkbox").html("<span>✔</span>");
            // 勾选数据
            let dataStr = layui.sessionData(CACHE_MATERIAL_TABLE)[CACHE_MATERIAL_CHECKED_KEY];
            if (isEmpty(dataStr)) {
                return;
            }
            let materialList = JSON.parse(dataStr);
            let materialSet = new Set(materialList.map(material => material.materialCode));
            layui.each(res.data, function (index, item) {
                if (materialSet.has(item.materialCode)) {
                    $("div[lay-id='ID-material-table-edit'] td .layui-form-checkbox").eq(index).click();
                }
            });
        },
        error: function (res, msg) {
            console.log(res, msg)
        }
    });

    // 触发表格复选框选择
    table.on('checkbox(material-table-edit)', function (obj) {
        let materialCode = obj?.data?.materialCode;
        let dataStr = layui.sessionData(CACHE_MATERIAL_TABLE)[CACHE_MATERIAL_CHECKED_KEY];
        let materialList = [];
        if (!isEmpty(dataStr)) {
            materialList = JSON.parse(dataStr);
        }
        // 判断是选中还是取消
        if (obj.checked) {
            materialList = materialList.filter(item => item.materialCode !== materialCode);
            materialList.push(obj?.data);
        } else {
            materialList = materialList.filter(item => item.materialCode !== materialCode);
        }
        layui.sessionData(CACHE_MATERIAL_TABLE, {
            key: CACHE_MATERIAL_CHECKED_KEY,
            value: JSON.stringify(materialList)
        });
    });

    // 触发表格单选框选择
    table.on('radio(material-table-edit)', function (obj) {
        console.log(obj)
    });

    // 触发单元格工具事件
    table.on('tool(material-table-edit)', function (obj) {
        const data = obj.data;
        if (obj.event === 'delete') {
            layer.confirm('确定删除' + data.materialCode + '吗?', {icon: 3, title: '警告'}, function (index) {
                layer.close(index);
            });
        }
    });

    // 确认选择-回填选中的材料数据
    $("#btn-material-confirm").click(function () {
        let materialList = JSON.parse(layui.sessionData(CACHE_MATERIAL_TABLE)[CACHE_MATERIAL_CHECKED_KEY]);
        parent.layui.table.reload('id-material-table', {
            data: materialList
        });
        const index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    // 取消-关闭弹框
    $("#btn-material-cancel").click(function () {
        const index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
});